package com.xdl.flytest.controller;

import com.xdl.flytest.bean.InterfaceInfo;
import com.xdl.flytest.service.InterfaceService;
import com.xdl.flytest.util.JsonResponseMessage;
import com.xdl.flytest.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: 80736
 * @Date: 2019/1/18 12:35
 * @Description:
 */
@Controller
@RequestMapping("/mongo")
public class InterfaceMongoController {

    @Autowired
    private InterfaceService interfaceService;

    /**
     * 根据id查询接口信息
     *
     * @param id
     * @return
     */
    @GetMapping("/interface")
    @ResponseBody
    public Map<String, Object> getInterface(@RequestParam("id") String id) {
        InterfaceInfo info = interfaceService.getInterfaceInfoFromMongo(id);
        return new JsonResponseMessage(ResponseMsg.SUCCESS).addAttribute("info", info).returned();
    }

    /**
     * 新增接口信息
     *
     * @param interfaceInfo
     * @return
     */
    @PostMapping("/interface")
    @ResponseBody
    public Map<String, Object> addInterface(@RequestBody InterfaceInfo interfaceInfo) {
        interfaceService.addInterfaceToMongo(interfaceInfo);

        interfaceInfo.setExampleId(interfaceInfo.get_id());
        return new JsonResponseMessage(ResponseMsg.SUCCESS).addAttribute("interfaceInfo", interfaceInfo).returned();
    }

    /**
     * 更新接口信息
     *
     * @param interfaceInfo
     */
    @PutMapping("/interface")
    @ResponseBody
    public void updateInterfaceInfo(@RequestBody InterfaceInfo interfaceInfo) {
        interfaceService.updateInterfaceToMongo(interfaceInfo);
    }

    /**
     * 删除接口信息
     *
     * @param id
     */
    @DeleteMapping("/interface")
    @ResponseBody
    public void deleteInterfaceInfo(@RequestParam("id") String id) {
        interfaceService.removeInterfaceInfoFromMongoById(id);
    }
}
